package com.example.ftp.Controller.SiteManage;

import com.example.ftp.Beans.Site;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @author : yhp
 */

public class SiteInfo {

    @FXML
    TextField name;
    @FXML
    TextField host;
    @FXML
    TextField port;
    @FXML
    TextField userName;
    @FXML
    TextField password;
    @FXML
    TextField note;
    @FXML
    Button ensureBtn;
    @FXML
    Button cancelBtn;

    private boolean isCreating = false;

    private Site site;

    public static boolean isNumeric(String str){
        Pattern pattern =Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    public static boolean isIP(String str){
        for(int i=str.length();--i>=0;){
            int chr=str.charAt(i);
            if(chr == 46)
                return true;
            if(chr<48||chr>57)
                return false;
        }
        return true;
    }

    public void initialize(){
        //限制端口输入
        port.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
                if(!isNumeric(t1)){
                        port.setText(s);
                }else if(Integer.parseInt(t1)>65535){
                    port.setText(s);
                }
            }
        });
        //限制IP地址输入
        host.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
                if(!isIP(t1)){
                    host.setText(s);
                }
            }
        });
    }

    //确定
    public void ensure(){
        if(host.getText().isEmpty()||port.getText().isEmpty()
                ||userName.getText().isEmpty()) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("tip");
            alert.setContentText("The input cannot be empty. ");
            alert.setHeaderText("Warning");
            alert.showAndWait();
        }else {
            if (isCreating) {
                site = new Site(name.getText(), host.getText(), Integer.parseInt(port.getText()), userName.getText(), password.getText(), note.getText());
                Stage stage = (Stage) ensureBtn.getScene().getWindow();
                stage.close();
            }
            else {
                site.setIp(host.getText());
                site.setPort(Integer.parseInt(port.getText()));
                site.setName(name.getText());
                site.setUserName(userName.getText());
                site.setPassword(password.getText());
                site.setNote(note.getText());
                Stage stage = (Stage) ensureBtn.getScene().getWindow();
                stage.close();
            }
        }
    }

    //取消
    public void cancel(){
        site = null;
        Stage stage = (Stage) cancelBtn.getScene().getWindow();
        stage.close();
    }

    public void fill(Site site) {
        name.setText(site.getName());
        host.setText(site.getIp());
        port.setText(String.valueOf(site.getPort()));
        userName.setText(site.getUserName());
        password.setText(site.getPassword());
        note.setText(site.getNote());
    }

    public Site open(Site site) throws IOException {
        Stage stage =new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("site-info.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        SiteInfo ctrl = fxmlLoader.getController();
        stage.setScene(scene);

        // 新建站点
        if (site == null) {
            // 在确定时将按照这个状态来决定修改还是新建
            ctrl.isCreating = true;
        }
        // 修改已有站点
        else {
            ctrl.site = site;
            ctrl.fill(site);
        }

        stage.showAndWait();

        return ctrl.site;
    }
}
